package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hhs.storage.bean.InputParam;
import com.hhs.storage.bean.Participle;
import com.hhs.storage.bean.TypeDic;
import com.hhs.storage.dao.ParticipleDao;
import com.hhs.storage.service.ParticipleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.util.SysUtil;
import com.huaban.analysis.jieba.JiebaSegmenter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.hhs.storage.util.PinYinUtil.getPinYinHeadChar;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pxx
 * @since 2024-01-24
 */
@Service
@Transactional
public class ParticipleServiceImpl extends ServiceImpl<ParticipleDao, Participle> implements ParticipleService {

    @Override
    public Map<String, Object> getAll(Participle participle, InputParam inputParam) {
        QueryWrapper qw = new QueryWrapper<Participle>();
        List<Participle> list = this.list(qw);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", list);
        map.put("code", 1);
        return map;
    }

    @Override
    public Map<String, Object> add(Participle participle, InputParam inputParam) {
        QueryWrapper<Participle> qw=new QueryWrapper<>();
        List<String> list = new ArrayList<>();
        for(Participle p : this.list(qw)){
            list.add(p.getParticiple());
        }
        String content = participle.getName();
        JiebaSegmenter segmenter = new JiebaSegmenter();
        List<String> result = segmenter.sentenceProcess(content);
        for (String str : result) {
            if (str.length()>=2){
                if (list.indexOf(str)==-1){
                    participle.setId(SysUtil.UUID());
                    participle.setInitial(getPinYinHeadChar(str));
                    participle.setParticiple(str);
                    this.save(participle);
                }

            }
        }



        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 1);
        return map;
    }
}
